﻿package mortal.game.control
{
    import Framework.Util.*;
    
    import Message.Game.*;
    import Message.Login.*;
    
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
	import com.gengine.game.*;
    
    import extend.php.*;
    
    import flash.display.*;
    
    import mortal.common.global.*;
    import mortal.common.preLoadPage.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.net.rmi.*;
    import mortal.game.resource.*;
    import mortal.game.scene.*;
    import mortal.mvc.core.*;

    public class LoginController extends Controller
    {
        public var stage:DisplayObjectContainer;
        private var _sloginGameReturn:SLoginGameReturn
        public var isLoginGameScuess:Boolean = false;
        private var _mapID:int = 300103;
        private var _currentGridX:int = 200;
        private var _currentGridY:int = 200;
        private static var _instance:LoginController;

        public function LoginController()
        {
            if (_instance)
            {
                throw new Error("LoginController 单例！");
            }
            
        }

        public function loginException(param1:Exception) : void
        {
            
        }

        public function loadGame() : void
        {
			
            var sloginReturn:SLoginReturn = PreLoginProxy.instance.sloginReturn;
            GameRMI.instance.rmiSession = PreLoginProxy.instance.gameSession.gmaeSession;
            GameRMI.instance.loginGameProxy.loginGame_async(new AMI_ILoginGame_loginGame(this.loginGameSuccess, this.loginGameException), sloginReturn.playerId, ParamsConst.instance.username, sloginReturn.name, sloginReturn.sessionKey.id, sloginReturn.sessionKey.key, ParamsConst.instance.version);
            if (PHPSender.isCreateRole)
            {
                PHPSender.sendToURLByPHP(SenderType.LoginGame);
            }
            Cache.instance.login.loginData = PreLoginProxy.instance.slogin;
            Cache.instance.login.loginReturn = sloginReturn;
        }
		
        private function loginGameSuccess(obj:Object, sloginreturn:SLoginGameReturn) : void
        {
            PHPSender.sendGift();
            if (PHPSender.isCreateRole)
            {
                PHPSender.sendToURLByPHP(SenderType.LoginGameSuccess);
            }
			
            PHPSender.isCreateRole = false;
            ClockManager.instance.setNowTimer(sloginreturn.sysDt);
            this._sloginGameReturn = sloginreturn;
            Cache.instance.login.loginGame = sloginreturn;
            this._mapID = this._sloginGameReturn.pos.map;
            this._currentGridX = this._sloginGameReturn.pos.x;
            this._currentGridY = this._sloginGameReturn.pos.y;
            this.initApp();
            GameManager.instance.init();
            Dispatcher.dispatchEvent(new DataEvent(EventName.LoginGameSuccess));
        }
		
		private function initApp() : void
		{
			this.initConfig();
			trace(" initApp ");
			GameLoadBar.instance.hide();
			this.initUI();
			this.initModules();
			this.initMap();
		}
		
		private function initConfig() : void
		{
			FilterText.instance.setFilterStr(ConfigManager.instance.getStringByFileName("filterChat.txt"));
			var _loc_1:* = ConfigManager.instance.getObjectByFileName("ChatFraudFilter.xml");
			if (_loc_1.filter)
			{
				if (_loc_1.filter is Array)
				{
					ChatFraudFilter.source = _loc_1.filter as Array;
				}
				else
				{
					ChatFraudFilter.source = [_loc_1.filter];
				}
			}
			NPCConfig.instance.init();		
		}
		
		private function initUI() : void
		{
			GameController.gameui.view.show();
			GameLayout.instance.init();
		}
		
		private function initModules() : void
		{
			ModuleConfig.instance.init();
			trace(" initModules ");
		}
		
        private function initMap() : void
        {
            MapConfig.mapPath = PathConst.mapPath;
            MapConfig.modelPath = PathConst.modelPath;
            Game.scene = new GameScene(Global.stage);
            Game.scene.setPlayerPoint(this._mapID, this._currentGridX, this._currentGridY);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ChangeScene));
            Log.debug("地图ID:" + this._mapID);
            GameLayout.instance.resize(true);
            
        }
		
        public function loginGameException(exceptino:Exception) : void
        {
            trace(exceptino.message);
            MsgManager.alertError(exceptino.code);
            if (PHPSender.isCreateRole)
            {
                PHPSender.sendToURLByPHP(SenderType.LoginGameFail, exceptino.message);
            }
            GameLoadBar.instance.hide();
           
        }

        public static function get instance() : LoginController
        {
            if (!_instance)
            {
                _instance = new LoginController;
            }
            return _instance;
        }

    }
}
